/*
 * @Author: Starfish_DL
 * @Date: 2020-11-15 00:12:17
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-11-15 00:29:04
 * @CurrentVersion: 1.0
 * @Description: 
 */

class MyCountThread2 extends Thread {
    private static volatile int count = 1;
    private int nextFlag;
    private Object lock;

    public MyCountThread2(String name, Object lock, int next) {
        super(name);
        this.lock = lock;
        this.nextFlag = next;
    }

    @Override
    public void run() {
        while (count <= 75) {
            synchronized (lock) {
                if (this.nextFlag != count) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    for (int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName() + ":" + count);
                        count++;
                        if (count > 75) {
                            break;
                        }
                    }
                    this.nextFlag = count + 10;
                    lock.notifyAll();
                }
            }
        }
    }
}

public class Test002 {

    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        MyCountThread2 th1 = new MyCountThread2("thread1", lock, 1);
        MyCountThread2 th2 = new MyCountThread2("thread2", lock, 6);
        MyCountThread2 th3 = new MyCountThread2("thread3", lock, 11);

        th1.start();
        th2.start();
        th3.start();
    }
}
